package com.tjx.controller;


import com.tjx.common.Result;
import com.tjx.entity.Coupon;
import com.tjx.service.ICouponService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 优惠券表 前端控制器
 * </p>
 *
 * @author tjx
 * @since 2025-10-29
 */
@RestController
@RequestMapping("/coupon")
public class CouponController {
    @Autowired
    private ICouponService couponService;
    // 领取优惠券
    @PostMapping("/receive")
    public Result receive(@RequestParam String couponId, @RequestParam String userId) {
        try {
            boolean success = couponService.couponDeductStock(couponId, userId);
            return success ? Result.ok("领取成功") : Result.fail("领取失败");
        } catch (Exception e) {
            return Result.fail(e.getMessage());
        }
    }


    //预热优惠券
    @PostMapping("/preheat")
    public Result preheat(@RequestBody Coupon coupon) {
        try {
            couponService.couponPreheatAdd(coupon);
            return Result.ok("预热成功");
        } catch (Exception e) {
            return Result.fail(e.getMessage());
        }
    }


}
